/*
 *  AMPLogDataRecord.cpp
 *  AMPlugin
 *
 *  Created by Toan on 7/6/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */
#include "VCPlugInHeaders.h"
#include "AMPLogDataRecord.h"
#include "AMGlobal.h"
#include "JSONHelper.h"
#include <fstream>

/********************************************************************************************************************
 *****Function: LogHeaderFileSave
 *****Creator:  Toan Nguyen
 *****IN: AMPLogDataRecordHeader* pHeader
 *****out: 
 *****Objective: load header
 ********************************************************************************************************************/
bool LogHeaderFileSave(AMPLogDataRecordHeader* pHeader )
{
	FILE *fn;
	string logDirectory;
	
	if (pHeader == NULL)
		return FALSE;
	
	logDirectory = g_System_logDirectory + "/" + g_Username + "@" + g_Domain;
	
	fn = fopen(logDirectory.c_str(), "wb");
	if (fn == NULL) {
		cout << "usename@domain file is nil" << endl;
		fclose(fn);
		return FALSE;
	}
	string _textframe = pHeader->g_TextFrames + "[[[/TextFrames]]]";
	//cout << "LogHeaderFileSave" << endl;
	
	fprintf(fn, "#Audience Media Header Record#\r\n");
	fprintf(fn, "System Date Year : %02d\r\n",				pHeader->DateYear);
	fprintf(fn, "System Date Month : %02d\r\n",				pHeader->DateMonth);
	fprintf(fn, "System Date Day : %02d\r\n",				pHeader->DateDay);
	
	fprintf(fn, "System Time Hour : %02d\r\n",				pHeader->TimeHour);
	fprintf(fn, "System Time Minute : %02d\r\n",			pHeader->TimeMinute);
	
	//fprintf(fn, "Total magazine count : %d\r\n",			pHeader->g_Magazine_Count);
	fprintf(fn, "Directory AM Plugin : %s\r\n",				pHeader->g_SDK_WorkspaceDir.c_str());
	fprintf(fn, "System Directory AM Plugin : %s\r\n",		pHeader->g_System_WorkspaceDir.c_str());
	fprintf(fn, "Current domain work : %s\r\n",				pHeader->g_Domain.c_str());
	//fprintf(fn, "User name ID : %s\r\n",					pHeader->g_Username.c_str());
	//fprintf(fn, "Is system Log in : %d\r\n",				pHeader->g_IsLoggedIn);
	fprintf(fn, "Is system Check out : %d\r\n",				pHeader->g_IsCheckOut);
	fprintf(fn, "Should download : %d\r\n",					pHeader->g_ShouldDownload);
	//fprintf(fn, "Text now selected : %d\r\n",				pHeader->g_TextSelected);
	//fprintf(fn, "Selected Text Frame UID : %s\r\n",			pHeader->g_SelectedTextFrameUID.c_str());

	fprintf(fn, "Text Frame Count : %d\r\n",				pHeader->g_TextFrameCount);
	fprintf(fn, "Current magazine ID : %s\r\n",				pHeader->g_Magazineid.c_str());
	fprintf(fn, "Current Issue ID : %s\r\n",				pHeader->g_Issueid.c_str());
	fprintf(fn, "Current Page ID : %s\r\n",					pHeader->g_Pageid.c_str());
	fprintf(fn, "Current Story ID : %s\r\n",				pHeader->g_Storyid.c_str());
	fprintf(fn, "Current IDML zip ID : %s\r\n",				pHeader->g_IDMLzip.c_str());
	fprintf(fn, "Current package ID : %s\r\n",				pHeader->g_Package_id.c_str());
	fprintf(fn, "Current document name : %s\r\n",			pHeader->g_DocName.c_str());
	fprintf(fn, "Current IDML open ID : %s\r\n",			pHeader->g_IDMLOpen.c_str());
	fprintf(fn, "Should Upload Document Name : %d\r\n",		pHeader->g_ShouldUpdateDocName);
	fprintf(fn, "Issue Setting : %d\r\n",					pHeader->g_IsIssueEnable);
	fprintf(fn, "Session connection : %s\r\n",				pHeader->n_mSessionConnection.c_str());
	fprintf(fn, "Session id : %s\r\n",						pHeader->n_mSessionID.c_str());
	fprintf(fn, "Session time out : %d\r\n",				pHeader->n_mSessionTimeOut);
	fprintf(fn, "Session user id : %s\r\n",					pHeader->n_mSessionUserID.c_str());
	fprintf(fn, "Session Counter Reset : %d\r\n",			pHeader->n_mSessionCounterReset);
	fprintf(fn, "Session Total Counter Reset : %d\r\n",		pHeader->n_mSessionTotalCounterReset);
	
	fprintf(fn, "Text frames : %s\r\n",						_textframe.c_str());//pHeader->g_TextFrames.c_str());
	//fprintf(fn, "Text frames : %s\r\n",						pHeader->g_Images.c_str());
	fprintf(fn, "issue editing: %s\r\n", g_issueEditing.c_str());
	fprintf(fn, "page editing: %s\r\n", g_pageEditing.c_str());
	
	//fprintf(fn, "backup here : \r\n");
	//fprintf(fn, "backup here : \r\n");
	//fprintf(fn, "backup here : \r\n");
	
	fclose(fn);
	
	return TRUE;
}


/********************************************************************************************************************
 *****Function: LogHeaderFileClear
 *****Creator:  Toan Nguyen
 *****IN: AMPLogDataRecordHeader* pHeader
 *****out: 
 *****Objective: clear header
 ********************************************************************************************************************/
bool LogHeaderFileClear(AMPLogDataRecordHeader* pHeader)
{
	//cout << "LogHeaderFileClear" << endl;
	if (pHeader == NULL)
		return FALSE;
	
	pHeader->DateYear					= 0;
	pHeader->DateMonth					= 0;
  	pHeader->DateDay					= 0;
  	pHeader->TimeHour					= 0;
  	pHeader->TimeMinute					= 0;
	
	//pHeader->g_Magazine_Count			= 0;
	pHeader->g_Domain					= "";
	//pHeader->g_Username					= "";
	//pHeader->g_IsLoggedIn				= FALSE;
	pHeader->g_ShouldDownload			= FALSE;
	//pHeader->g_TextSelected				= FALSE;
	//pHeader->g_SelectedTextFrameUID		= "";
	pHeader->g_TextFrameCount			= 0;

	pHeader->g_IsCheckOut				= FALSE;
	pHeader->g_IsIssueEnable			= FALSE;

	pHeader->g_Magazineid				= "";
	pHeader->g_Issueid					= "";
	pHeader->g_Pageid					= "";
	pHeader->g_Storyid					= "";
	pHeader->g_IDMLzip					= "";
	pHeader->g_Package_id				= "";
	pHeader->g_DocName					= "";
	pHeader->g_IDMLOpen					= "";
	pHeader->g_ShouldUpdateDocName		= FALSE;
	
	pHeader->n_mSessionConnection		= "";
	pHeader->n_mSessionID				= "";
	pHeader->n_mSessionTimeOut			= 0;
	pHeader->n_mSessionUserID			= "";
	pHeader->n_mSessionCounterReset		= 0;
	pHeader->n_mSessionTotalCounterReset = 18000; // setting time to request connect to CMS
	pHeader->g_TextFrames				= "";
	return TRUE;
}


/********************************************************************************************************************
 *****Function: LogHeaderFileSetValue
 *****Creator:  Toan Nguyen
 *****IN: AMPLogDataRecordHeader* pHeader
 *****out: 
 *****Objective: set value
 ********************************************************************************************************************/
bool LogHeaderFileSetValue(AMPLogDataRecordHeader* pHeader)
{
	//if (pHeader == NULL)
	//	return FALSE;
	//cout << "LogHeaderFileSetValue" << endl;
	
	unsigned long hour, minute, second;
	unsigned long year, month, day;
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	
	//Read year month and day
	year = timeinfo->tm_year + 1900;
	month = timeinfo->tm_mon + 1;
	day = timeinfo->tm_mday;
	//Read Hour minute and second
	hour = timeinfo->tm_hour;
	minute = timeinfo->tm_min;
	//second = timeinfo->tm_sec;
	
	pHeader->DateYear					= year;
	pHeader->DateMonth					= month;
  	pHeader->DateDay					= day;
  	pHeader->TimeHour					= hour;
  	pHeader->TimeMinute					= minute;
	
	//pHeader->g_Magazine_Count			= g_Magazine_Count;
	pHeader->g_SDK_WorkspaceDir			= g_SDK_WorkspaceDir;
	pHeader->g_System_WorkspaceDir		= g_System_WorkspaceDir;
	
	pHeader->g_Domain					= g_Domain;
	//pHeader->g_Username					= g_Username;
	//pHeader->g_IsLoggedIn				= g_IsLoggedIn;
	//pHeader->g_IsCheckOut				= g_IsCheckOut;
	//pHeader->g_ShouldDownload			= g_ShouldDownload;
	//pHeader->g_TextSelected				= g_TextSelected;
	//pHeader->g_SelectedTextFrameUID		= g_SelectedTextFrameUID;
	pHeader->g_TextFrameCount			= g_TextFrameCount;
	
	//pHeader->g_IsIssueEnable			= g_IsIssueEnable;
	
	pHeader->g_Magazineid				= g_Magazineid;
	pHeader->g_Issueid					= g_Issueid;
	pHeader->g_Pageid					= g_Pageid;
	pHeader->g_Storyid					= g_Storyid;
	pHeader->g_IDMLzip					= g_IDMLzip;
	pHeader->g_Package_id				= g_Package_id;
	pHeader->g_DocName					= g_DocName;
	//pHeader->g_IDMLOpen					= g_IDMLOpen;
	//pHeader->g_ShouldUpdateDocName		= g_ShouldUpdateDocName;
	
	pHeader->n_mSessionConnection		= "AudienceMedia Connection";
	pHeader->n_mSessionID				= n_mSessionID;
	pHeader->n_mSessionTimeOut			= n_mSessionTimeOut;
	pHeader->n_mSessionUserID			= n_mSessionUserID;
	pHeader->n_mSessionCounterReset		= n_mSessionCounterReset;
	pHeader->n_mSessionTotalCounterReset = n_mSessionTotalCounterReset;
	
	string tmp = "";
	for (int i = 0; i < g_TextFrameCount; i++) {
		//tmp = tmp + g_TextFrames[i].textframeUID + "/" + g_TextFrames[i].storyID + "/" + g_TextFrames[i].sessionID + "/" + g_TextFrames[i].storyName;
		tmp = tmp + g_TextFrames[i].textframeUID + "/" + g_TextFrames[i].storyID + "/" + g_TextFrames[i].sessionID + "/" + g_TextFrames[i].storyName + "/" + g_TextFrames[i].storySelf;
		//cout << "==== tmp: " << tmp << endl;
		if (g_TextFrames[i].isLocked == TRUE) {
			tmp = tmp + "/TRUE" + ";";  
		}
		else {
			tmp = tmp + "/FALSE" + ";";
		}
	}
	pHeader->g_TextFrames				= tmp;
	
	string imgTmp = "";
	return TRUE;
}


/********************************************************************************************************************
 *****Function: LogHeaderFileInitValue
 *****Creator:  Toan Nguyen
 *****IN: AMPLogDataRecordHeader* pHeader
 *****out: 
 *****Objective: init value
 ********************************************************************************************************************/
bool LogHeaderFileInitValue(AMPLogDataRecordHeader* pHeader)
{
	if (pHeader == NULL)
		return FALSE;
	//cout << "LogHeaderFileInitValue" << endl;
	
	//g_Magazine_Count			= pHeader->g_Magazine_Count;
	//g_Domain					= pHeader->g_Domain;
	//g_Username					= pHeader->g_Username;
	//g_IsLoggedIn				= pHeader->g_IsLoggedIn;
	//g_ShouldDownload			= pHeader->g_ShouldDownload;
	//g_TextSelected				= pHeader->g_TextSelected;
	//g_SelectedTextFrameUID		= pHeader->g_SelectedTextFrameUID;
	g_TextFrameCount			= pHeader->g_TextFrameCount;

	//g_IsCheckOut				= pHeader->g_IsCheckOut;
	//g_IsIssueEnable				= pHeader->g_IsIssueEnable;
/*	
	//g_Magazineid				= pHeader->g_Magazineid;
	g_Issueid					= pHeader->g_Issueid;
	g_Pageid					= pHeader->g_Pageid;
	g_Storyid					= pHeader->g_Storyid;
	g_IDMLzip					= pHeader->g_IDMLzip;
	g_Package_id				= pHeader->g_Package_id;
 */
	g_DocName					= pHeader->g_DocName;
	//g_IDMLOpen					= pHeader->g_IDMLOpen;
	//g_ShouldUpdateDocName		= pHeader->g_ShouldUpdateDocName;

	n_mSessionConnection		= pHeader->n_mSessionConnection;
	n_mSessionID				= pHeader->n_mSessionID;
	n_mSessionTimeOut			= pHeader->n_mSessionTimeOut;
	n_mSessionUserID			= pHeader->n_mSessionUserID;
	n_mSessionCounterReset		= pHeader->n_mSessionCounterReset;
	n_mSessionTotalCounterReset = pHeader->n_mSessionTotalCounterReset;

	string tmp = pHeader->g_TextFrames;
	//cout << "original tmp: " << tmp << endl;
	size_t pos;
	if (g_TextFrames != NULL) {
		delete [] g_TextFrames;
	}

	g_TextFrames = new textframes_t[g_TextFrameCount+1];

	for (int i = 0; i < g_TextFrameCount; i++) {
		// textframeUID

		pos = tmp.find("/");
		g_TextFrames[i].textframeUID = tmp.substr(0, pos);
		//cout << "g_TextFrames[i].textframeUID: " << g_TextFrames[i].textframeUID << endl;
		tmp = tmp.substr(pos+1);

		// storyID
		pos = tmp.find("/");
		g_TextFrames[i].storyID = tmp.substr(0, pos);
		//cout << "g_TextFrames[i].storyID:" << g_TextFrames[i].storyID << endl; 
		tmp = tmp.substr(pos+1);

		// session ID of story
		pos = tmp.find("/");
		g_TextFrames[i].sessionID = tmp.substr(0, pos);
		//cout << "g_TextFrames[i].sessionID" << g_TextFrames[i].sessionID << endl; 
		tmp = tmp.substr(pos+1);

		// storyName
		pos = tmp.find("/");
		g_TextFrames[i].storyName = tmp.substr(0, pos);
		tmp = tmp.substr(pos+1);
		
		// storySelf
		pos = tmp.find("/");
		g_TextFrames[i].storySelf = tmp.substr(0, pos);
		tmp = tmp.substr(pos+1);
		
		// lock status
		pos = tmp.find(";");

		if (tmp.substr(0, pos) == "TRUE") {
			g_TextFrames[i].isLocked = TRUE;
		}
		else {
			g_TextFrames[i].isLocked = FALSE;
		}
		
		//cout << "tmp.substr(0, pos) :" << tmp.substr(0, pos) << endl; 
		//cout << "tmp.length - pos: " << tmp.length() << " - " << pos << endl;
		if (pos < tmp.length()) {
			tmp = tmp.substr(pos);
		}
		
	}
	return TRUE;
}


/********************************************************************************************************************
 *****Function: LogHeaderFileLoad
 *****Creator:  Toan Nguyen
 *****IN: AMPLogDataRecordHeader* pHeader
 *****out: 
 *****Objective:
 ********************************************************************************************************************/
bool LogHeaderFileLoad(AMPLogDataRecordHeader* pHeader)
{
	
	FILE	*fn;
	char	temp[4096];
	bool	ret = FALSE;
	int		line;
	char	c;
	string logDirectory;
	bool	StatusRead = FALSE;
	//cout << "LogHeaderFileLoad" << endl;
	logDirectory = g_System_logDirectory + "/" + g_Username + "@" + g_Domain;
	cout << "logDirectory: " << logDirectory << endl;
	
	if (pHeader == NULL)
	{
		cout << "header Null - exit" << endl;
		return FALSE;
	}
	
	fn = fopen(logDirectory.c_str(), "rb");
	if (fn == NULL) 
	{
		cout << "cannot open file log " << endl;
		fclose(fn);
		return FALSE;
	}

	fscanf(fn, "%s", temp);
	if (strncmp("#Audience", temp, 9) != 0)
		goto loadheader_exit;
	fscanf(fn, " %s", temp);
	if (strncmp("Media", temp, 5) != 0)
		goto loadheader_exit;

	fscanf(fn, " %s\n", temp);
	if (strncmp("Header", temp, 6) != 0)
		goto loadheader_exit;
	
	fscanf(fn, " %s\n", temp);
	if (strncmp("Record#", temp, 7) != 0)
		goto loadheader_exit;
	
	while (1) {
		line = 0;
		while (line<255) {
			if (feof(fn)) goto loadheader_exit;
			temp[line] = getc(fn);
			if ((line == 0) && ((temp[0] == '\r') || (temp[0] == '\n')))
				continue;
			if (temp[line] == ':')
				break;
			line++;
		}
			// System Year:month:day
		if (strncmp("System Date Year", temp, 16) == 0) {
			
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->DateYear);

		} else if (strncmp("System Date Month", temp, 17) == 0) {
				line = 0;
				while (line<255) {
					if (feof(fn)) goto loadheader_exit;
					c = getc(fn);
					if ((c != ' ') && (c != '\t')) 
						break;
					line++;
				}
				if ((c == '\n') || (c == '\r')) {
					continue;
				}
				
				fseek(fn, -1, SEEK_CUR);
				fscanf(fn, "%d\n", &pHeader->DateMonth);
		
		} else if (strncmp("System Date Day", temp, 15) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->DateDay);
			// System time
		} else if (strncmp("System Time Hour", temp, 16) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->TimeHour);
		
		} else if (strncmp("System Time Minute", temp, 18) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->TimeMinute);
			
			// Total magazine count :
		} /*else if (strncmp("Total magazine count", temp, 20) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				//pHeader->g_Magazine_Count = 0;
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			//fscanf(fn, "%d\n", &pHeader->g_Magazine_Count);

			// Directory AM Plugin :
		} */else if(strncmp("System Directory AM Plugin", temp, 26) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_SDK_WorkspaceDir = temp;

			// System Directory AM Plugin :
		} else if (strncmp("Directory AM Plugin", temp, 19) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_System_WorkspaceDir = temp;
	
			// Current domain work :
		} else if (strncmp("Current domain work", temp, 19) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_Domain = temp;

			// Username :
		} /* else if (strncmp("User name ID", temp, 12) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_Username = temp;
		
			// Is system Log in :
		} else if (strncmp("Is system Log in", temp, 16) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			//fscanf(fn, "%d\n", &pHeader->g_IsLoggedIn);
	
			// Is systen Check out :
		} */else if (strncmp("Is system Check out", temp, 19) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->g_IsCheckOut);
		
			// Should download :
		} else if (strncmp("Should download", temp, 15) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->g_ShouldDownload);

			//Text now selected
		}/* else if (strncmp("Text now selected", temp, 17) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			//fscanf(fn, "%d\n", &pHeader->g_TextSelected);

			// Selected Text Frame UID :
		}*//* else if (strncmp("Selected Text Frame UID", temp, 23) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			//pHeader->g_SelectedTextFrameUID = temp;
		
			// Text Frame Count :
		} */else if (strncmp("Text Frame Count", temp, 16) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->g_TextFrameCount);
	
			// Current magazine ID :
		} else if (strncmp("Current magazine ID", temp, 19) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_Magazineid = temp;

			// Current Issue ID :
		} else if (strncmp("Current Issue ID", temp, 16) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_Issueid = temp;
		
			// Current Page ID :
		} else if (strncmp("Current Page ID", temp, 15) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_Pageid = temp;

			// Current Story ID :
		} else if (strncmp("Current Story ID", temp, 16) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_Storyid = temp;
	
			// Current IDML zip ID :
		} else if (strncmp("Current IDML zip ID", temp, 19) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_IDMLzip = temp;
		
			// Current package ID :
		} else if (strncmp("Current package ID", temp, 18) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_Package_id = temp;
		
			// Current document name :
		} else if (strncmp("Current document name", temp, 11) == 0) {
			line = 0;
			while (line<2048) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<2048) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_DocName = temp;
			
			// Current IDML open ID :
		} else if (strncmp("Current IDML open ID", temp, 20) == 0) {
			line = 0;
			while (line<2048) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<2048) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->g_IDMLOpen = temp;

			// Should Upload Document Name :
		} else if (strncmp("Should Upload Document Name", temp, 26) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->g_ShouldUpdateDocName);
			
			// Issue Setting :
		}/* else if (strncmp("Issue Setting", temp, 13) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->g_IsIssueEnable);
		
			// Session id :
		}*/ else if (strncmp("Session id", temp, 10) == 0) {
			line = 0;
			while (line<2048) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<2048) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->n_mSessionID = temp;
			
			// Session time out :
		} else if (strncmp("Session time out", temp, 16) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->n_mSessionTimeOut);
		
			// Session user id :
		} else if (strncmp("Session user id", temp, 15) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			pHeader->n_mSessionUserID = temp;
			
			//Session Counter Reset :
		} else if (strncmp("Session Counter Reset", temp, 21) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->n_mSessionCounterReset);
			
		} else if (strncmp("Session Total Counter Reset", temp, 27) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &pHeader->n_mSessionTotalCounterReset);
		
			// Text frames :
		} else if (strncmp("Text frames", temp, 11) == 0) {
				line = 0;
			
				string strRead = "";
				while(TRUE) {
					if (feof(fn)) {
						StatusRead = TRUE;
						goto loadheader_exit;
					}
					c = getc(fn);
					
				//	if ((c != ' ') && (c != '\t')) 
				//		break;
					
					line++;
					strRead += c;
					if(strRead.find("[[[/TextFrames]]]") != string::npos)
					{
						//cout << "++++strRead:" << strRead << endl;
						break;
					}
				}
				pHeader->g_TextFrames = strRead.substr(0, strRead.find("[[[/TextFrames]]]"));
			/*
				while (line<4096) {
					if (feof(fn)) goto loadheader_exit;
					c = getc(fn);
					if ((c != ' ') && (c != '\t')) 
						break;
					line++;
				}
				if ((c == '\n') || (c == '\r'))
					continue; 
				temp[0] = c;
				line = 1;
				while (line<4096) {
					if (feof(fn)) goto loadheader_exit;
					c = getc(fn);
					temp[line] = c;
					if ((c == '\n') || (c == '\r'))
						break;
					
					line++;
				}
			 */
				//temp[line] = '\0';
				//pHeader->g_TextFrames = temp;
				//cout << "pHeader->g_TextFrames:" << pHeader->g_TextFrames << endl;
			
		} else if (strncmp("issue editing", temp, 13) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_issueEditing = temp;
			
		} else if (strncmp("page editing", temp, 12) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_pageEditing = temp;
			
		} else {
			line = 0;
			while (line<255) {
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				line++;
			}
		}

		ret = TRUE;
		if (feof(fn)) break;
	}
	
	cout << "================ Para pHeader pointer - value: " << &pHeader << " - " << pHeader << endl;

loadheader_exit:
	//cout << "================ loadheader_exit pHeader pointer - value: " << &pHeader << " - " << pHeader << endl;
	if(StatusRead == TRUE)
	{
		pHeader->g_TextFrames = "";
		g_TextFrameCount = 0;
	}
	fclose(fn);
	return ret;
}


/********************************************************************************************************************
 *****Function: InitLogHeaderFile
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
int InitLogHeaderFile()
{
	bool retCode = FALSE;
	//cout << "Allocate memory" << endl;
	//Load header file set before
	AMPLogDataRecordHeader* pHeader = new AMPLogDataRecordHeader;
	retCode = LogHeaderFileClear(pHeader);
	//cout << "Allocate memory 1" << endl;
	retCode = LogHeaderFileLoad(pHeader);
	//cout << "Allocate memory 2" << endl;
	retCode = LogHeaderFileInitValue(pHeader);
	//cout << "Allocate memory 3" << endl;
	LogHeaderFileDebug(pHeader);
	//cout << "Allocate memory 4" << endl;
	delete pHeader;
	//cout << "Allocate memory 5" << endl;
	return retCode;
}


/********************************************************************************************************************
 *****Function: SaveLogHeaderFile
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
int SaveLogHeaderFile()
{
	bool retCode;
	//Save header file set before 
	AMPLogDataRecordHeader* pHeader = new AMPLogDataRecordHeader;
	retCode = LogHeaderFileClear(pHeader);
	retCode = LogHeaderFileSetValue(pHeader);
	retCode = LogHeaderFileSave(pHeader);
	//LogHeaderFileDebug(pHeader);
	delete pHeader;
	return retCode;
}


/********************************************************************************************************************
 *****Function: LogHeaderFileDebug
 *****Creator:  Toan Nguyen
 *****IN: AMPLogDataRecordHeader* pHeader
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
void LogHeaderFileDebug(AMPLogDataRecordHeader* pHeader)
{
#ifdef _INDESIG_DEBUG
	/*
	cout <<  "#Audience Media Header Record#" << endl;
	cout <<  "System Date : " << pHeader->DateYear << "/" << pHeader->DateMonth << "/" << pHeader->DateDay<< endl;
	cout <<  "System Time : " << pHeader->TimeHour << "/" << pHeader->TimeMinute << endl;
	//cout <<  "Total magazine count : " << pHeader->g_Magazine_Count<< endl;
	cout <<  "Directory AM Plugin : " << 	pHeader->g_SDK_WorkspaceDir.c_str() << endl;
	cout <<  "System Directory AM Plugin : " << pHeader->g_System_WorkspaceDir.c_str() << endl;
	cout <<  "Current domain work : " << 	pHeader->g_Domain.c_str() << endl;
	cout <<  "User name ID : " << pHeader->g_Username.c_str() << endl;
	//cout <<  "Is system Log in : " << pHeader->g_IsLoggedIn << endl;
	cout <<  "Is systen Check out : " << 	pHeader->g_IsCheckOut << endl;
	cout <<  "Should download : " << pHeader->g_ShouldDownload << endl;
	//cout <<  "Text now selected : " << pHeader->g_TextSelected << endl;
	//cout <<  "Selected Text Frame UID : " << pHeader->g_SelectedTextFrameUID.c_str() << endl;
	
	cout <<  "Text Frame Count : " << pHeader->g_TextFrameCount << endl;
	cout <<  "Current magazine ID : " << 	pHeader->g_Magazineid.c_str() << endl;
	cout <<  "Current Issue ID : " << pHeader->g_Issueid.c_str() << endl;
	cout <<  "Current Page ID : " << 	pHeader->g_Pageid.c_str() << endl;
	cout <<  "Current Story ID : " << pHeader->g_Storyid.c_str() << endl;
	cout <<  "Current IDML zip ID : " <<	pHeader->g_IDMLzip.c_str() << endl;
	cout <<  "Current package ID : " << pHeader->g_Package_id.c_str() << endl;
	cout <<  "Current document name : " << pHeader->g_DocName.c_str() << endl;
	cout <<  "Current IDML open ID : " << pHeader->g_IDMLOpen.c_str() << endl;
	cout <<  "Should Upload Document Name : " <<	pHeader->g_ShouldUpdateDocName << endl;
	cout <<  "Issue Setting : " << pHeader->g_IsIssueEnable << endl;
	cout <<  "Session connection : " << 	pHeader->n_mSessionConnection.c_str() << endl;
	cout <<  "Session ID : " << 	pHeader->n_mSessionID.c_str() << endl;
	cout <<  "Session TimeOut : " << 	pHeader->n_mSessionTimeOut << endl;
	cout <<  "Session User id : " << 	pHeader->n_mSessionUserID.c_str() << endl;
	cout <<  "Session Counter Reset : " << 	pHeader->n_mSessionCounterReset << endl;
	cout <<  "Session Total Counter Reset : " << 	pHeader->n_mSessionTotalCounterReset << endl;
	 */
#endif
}

int LoadLogHeaderFile() {
	bool retCode;
	//Load header file set before
	AMPLogDataRecordHeader* pHeader = new AMPLogDataRecordHeader;
	//cout << "================ initial pHeader pointer - value: " << &pHeader << " - " << pHeader << endl;
	retCode = LogHeaderFileClear(pHeader);
	retCode = LogHeaderFileLoad(pHeader);
	//cout << "================ LogHeaderFileLoad pHeader pointer - value: " << &pHeader << " - " << pHeader << endl;
	if (retCode == TRUE && (g_Domain == pHeader->g_Domain)) {
		retCode = LogHeaderFileInitValue(pHeader);
	}
	//LogHeaderFileDebug(pHeader);
	delete pHeader;
	return retCode;
}

/********************************************************************************************************************
 *****Function: ResetGlobalValue
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
bool ResetGlobalValue()
{
	g_IsLoggedIn				= FALSE;
	//g_ShouldDownload			= FALSE;
	//g_TextSelected				= FALSE;
	//g_SelectedTextFrameUID		= "";
	//g_TextFrameCount			= 0;
	
	//g_IsCheckOut				= FALSE;
	//g_IsIssueEnable				= FALSE;
	
	//g_Magazineid				= "";
	//g_Issueid					= "";
	//g_Pageid					= "";
	//g_Storyid					= "";
	//g_IDMLzip					= "";
	//g_Package_id				= "";
	//g_DocName					= "none";
	//g_IDMLOpen					= "";
	//g_ShouldUpdateDocName		= FALSE;
	
	//n_mSessionConnection		= "";
	//n_mSessionID				= "";
	//n_mSessionTimeOut			= 18000;
	//n_mSessionUserID			= "";
	
	return TRUE;
}


/********************************************************************************************************************
 *****Function: SaveSessionInfo
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
int SaveSessionInfo() {
	FILE *fn;
	string sessionInfoPath;
	
	sessionInfoPath = g_System_WorkspaceDir + "/" + g_Username + ".info";
	//cout << "Save n_mSessionID: " << n_mSessionID << endl;
	
	fn = fopen(sessionInfoPath.c_str(), "wb");
	if (fn == NULL) {
		fclose(fn);
		return FALSE;
	}
	string _imagePath = g_CurPreviewImaPath + "[[[/ImagePaths]]]";
	fprintf(fn, "#Audience Media Header Record#\r\n");
	fprintf(fn, "Session id : %s\r\n", n_mSessionID.c_str());
	fprintf(fn, "Current Issue ID : %s\r\n", g_Issueid.c_str());
	fprintf(fn, "Current Page ID : %s\r\n", g_Pageid.c_str());
	fprintf(fn, "Current Story ID : %s\r\n", g_Storyid.c_str());
	fprintf(fn, "Current IDML zip ID : %s\r\n", g_IDMLzip.c_str());
	fprintf(fn, "Current package ID : %s\r\n", g_Package_id.c_str());
	//fprintf(fn, "Current IDML open ID : %s\r\n", g_IDMLOpen.c_str());
	fprintf(fn, "Document name : %s\r\n", g_DocName.c_str());
	//fprintf(fn, "Is idml check out : %d\r\n", g_IsCheckOut);
	fprintf(fn, "Page(s) check out : %s\r\n", g_pageNumberCheckOut.c_str());
	fprintf(fn, "Image preview path : %s\r\n", _imagePath.c_str());
	fprintf(fn, "Node name : %s\r\n", g_CurPreviewNodeName.c_str());
	fprintf(fn, "Resolution mode : %s\r\n", g_Resolution.c_str());
	fprintf(fn, "Unlock stories: %s\r\n", g_UnlockStories.c_str());
	//fprintf(fn, "issue editing: %s\r\n", g_issueEditing.c_str());
	//fprintf(fn, "page editing: %s\r\n", g_pageEditing.c_str());
	
	fclose(fn);
	//cout << "Save n_mSessionID: " << n_mSessionID << endl;
	return TRUE;
			
}

/********************************************************************************************************************
 *****Function: LoadSessionInfo
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/			
string LoadSessionInfo() {
	FILE	*fn;
	char	temp[4096];
	int		line;
	char	c;
	string res = "";
	string sessionInfoPath;
	
	sessionInfoPath = g_System_WorkspaceDir + "/" + g_Username + ".info";
		
	fn = fopen(sessionInfoPath.c_str(), "rb");
	if (fn == NULL) 
	{
		cout << "cannot open: " << sessionInfoPath << endl;
		fclose(fn);
		return res;
	}
	
	fscanf(fn, "%s", temp);
	if (strncmp("#Audience", temp, 9) != 0)
		goto loadheader_exit;
	fscanf(fn, " %s", temp);
	if (strncmp("Media", temp, 5) != 0)
		goto loadheader_exit;
	
	fscanf(fn, " %s\n", temp);
	if (strncmp("Header", temp, 6) != 0)
		goto loadheader_exit;
	
	fscanf(fn, " %s\n", temp);
	if (strncmp("Record#", temp, 7) != 0)
		goto loadheader_exit;
	
	while (1) {
		line = 0;
		while (line<255) {
			if (feof(fn)) goto loadheader_exit;
			temp[line] = getc(fn);
			if ((line == 0) && ((temp[0] == '\r') || (temp[0] == '\n')))
				continue;
			if (temp[line] == ':')
				break;
			line++;
		}
		// Session id :
		if (strncmp("Session id", temp, 10) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			//pHeader->n_mSessionID = temp;
			res = temp;
			
		} else if (strncmp("Current Issue ID", temp, 16) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_Issueid = temp;
			
			// Current Page ID :
		} else if (strncmp("Current Page ID", temp, 15) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_Pageid = temp;
			
			// Current Story ID :
		} else if (strncmp("Current Story ID", temp, 16) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_Storyid = temp;
			
			// Current IDML zip ID :
		} else if (strncmp("Current IDML zip ID", temp, 19) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_IDMLzip = temp;
			
			// Current package ID :
		} else if (strncmp("Current package ID", temp, 18) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_Package_id = temp;
		
			// Current document name :
		} else if (strncmp("Document name", temp, 13) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_DocName = temp;
		
		} else if (strncmp("Page(s) check out", temp, 17) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			string tmpspace = " ";
			g_pageNumberCheckOut = tmpspace + temp;

		} else if (strncmp("Image preview path", temp, 18) == 0) {
			line = 0;
			string strRead = "";
			while(TRUE) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				
				//	if ((c != ' ') && (c != '\t')) 
				//		break;
				
				line++;
				strRead += c;
				if(strRead.find("[[[/ImagePaths]]]") != string::npos)
				{
					//cout << "++++strRead:" << strRead << endl;
					break;
				}
			}
			g_CurPreviewImaPath = strRead.substr(0, strRead.find("[[[/ImagePaths]]]"));
			/*
			while (line<4096) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<4096) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_CurPreviewImaPath = temp;
			*/
		} else if (strncmp("Node name", temp, 8) == 0) {
			line = 0;
			while (line<2048) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<2048) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_CurPreviewNodeName = temp;
			
		} else if (strncmp("Resolution mode", temp, 9) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_Resolution = temp;
			// Is idml check out
		} /* else if (strncmp("Is idml check out", temp, 17) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			fscanf(fn, "%d\n", &g_IsCheckOut);
			
			// Current IDML open ID :
		} else if (strncmp("Current IDML open ID", temp, 20) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_IDMLOpen = temp;
		} */			
		
		else if (strncmp("Unlock stories", temp, 14) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_UnlockStories = temp;
		/*	
		} else if (strncmp("issue editing", temp, 13) == 0) {
				line = 0;
				while (line<255) {
					if (feof(fn)) goto loadheader_exit;
					c = getc(fn);
					if ((c != ' ') && (c != '\t')) 
						break;
					line++;
				}
				if ((c == '\n') || (c == '\r'))
					continue; 
				temp[0] = c;
				line = 1;
				while (line<255) {
					if (feof(fn)) goto loadheader_exit;
					c = getc(fn);
					temp[line] = c;
					if ((c == '\n') || (c == '\r'))
						break;
					
					line++;
				}
				temp[line] = '\0';
				g_issueEditing = temp;
				   
		} else if (strncmp("page editing", temp, 12) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_pageEditing = temp;
			*/
		}
		if (feof(fn)) break;
	}
	
loadheader_exit:
	if(g_pageNumberCheckOut == ""){
		g_Issueid = "";
		g_CurPreviewImaPath = "";
		g_CurPreviewNodeName = "";
		g_Resolution = "low";
		g_issueEditing = "";
		g_pageEditing = "";
	}
	
	fclose(fn);
	return res;
}

/********************************************************************************************************************
 *****Function: SaveSettingInfo
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
bool SaveSettingInfo() {
	FILE *fn;
	string logDirectory;
	
	logDirectory = g_System_logDirectory + "/Settings.info";
	//cout << "logDirectory: " << logDirectory << endl;
	fn = fopen(logDirectory.c_str(), "wb");
	if (fn == NULL) {
		//cout << "settings.info is null";
		fclose(fn);
		return FALSE;
	}
	
	fprintf(fn, "#Audience Media Settings#\r\n");
	fprintf(fn, "Latest Domain : %s\r\n", g_Domain.c_str());
	fprintf(fn, "Latest Username : %s\r\n",	g_Username.c_str());
	fprintf(fn, "Latest proxy server : %s\r\n",	g_ProxyServer.c_str());
	fprintf(fn, "Latest proxy port : %s\r\n",	g_ProxyPort.c_str());
	fprintf(fn, "Latest proxy user : %s\r\n",	g_ProxyUser.c_str());
	fprintf(fn, "Latest proxy pass : %s\r\n",	g_ProxyPass.c_str());
	fprintf(fn, "Latest proxy enable : %d\r\n",	g_ProxyEnable);
	fprintf(fn, "Latest proxy authen enable : %d\r\n",	g_ProxyAuthEnable);
	fclose(fn);
	
	return TRUE;
	
}


/********************************************************************************************************************
 *****Function: LoadSettingInfo
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
bool LoadSettingInfo() {
	
	FILE	*fn;
	char	temp[1025];
	int		line;
	char	c;
	string res = "";
	string sessionInfoPath;
	
	sessionInfoPath = g_System_logDirectory + "/Settings.info";
	//cout << "sessionInfoPath: " << sessionInfoPath << endl;
	
	fn = fopen(sessionInfoPath.c_str(), "rb");
	if (fn == NULL) 
	{
		//cout << "cannot open: " << sessionInfoPath << endl;
		fclose(fn);
		return FALSE;
	}
	
	fscanf(fn, "%s", temp);
	if (strncmp("#Audience", temp, 9) != 0)
		goto loadheader_exit;
	fscanf(fn, " %s", temp);
	if (strncmp("Media", temp, 5) != 0)
		goto loadheader_exit;
	
	fscanf(fn, " %s\n", temp);
	if (strncmp("Settings#", temp, 9) != 0)
		goto loadheader_exit;
	
	while (1) {
		line = 0;
		while (line<1024) {
			if (feof(fn)) goto loadheader_exit;
			temp[line] = getc(fn);
			if ((line == 0) && ((temp[0] == '\r') || (temp[0] == '\n')))
				continue;
			if (temp[line] == ':')
				break;
			line++;
		}
		// Latest Domain :
		if (strncmp("Latest Domain", temp, 13) == 0) {
			line = 0;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				line++;
			}
			temp[line] = '\0';
			g_Domain = temp;
			cout << "++++++++++++++++ g_Domain: " << g_Domain << endl;
			
		} else if (strncmp("Latest Username", temp, 15) == 0) {
			line = 0;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_Username = temp;
			cout << "++++++++++++++ g_Username: " << g_Username << endl;
			
			// ip proxy
		} else if (strncmp("Latest proxy server", temp, 18) == 0) {
			line = 0;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_ProxyServer = temp;
			// port proxy
		} else if (strncmp("Latest proxy port", temp, 17) == 0) {
			line = 0;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_ProxyPort = temp;
			// use auth
		} else if (strncmp("Latest proxy user", temp, 17) == 0) {
			line = 0;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_ProxyUser = temp;
			// pass auth
		} else if (strncmp("Latest proxy pass", temp, 17) == 0) {
			line = 0;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r'))
				continue; 
			temp[0] = c;
			line = 1;
			while (line<1024) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				temp[line] = c;
				if ((c == '\n') || (c == '\r'))
					break;
				
				line++;
			}
			temp[line] = '\0';
			g_ProxyPass = temp;
			// proxy enable
		} else if (strncmp("Latest proxy enable", temp, 19) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			int getvalue = 0;
			fscanf(fn, "%d\n", &getvalue);
			g_ProxyEnable = (bool)getvalue;
			//cout << "+++++g_ProxyEnable:" << g_ProxyEnable << endl;
			// proxy auth enable
		} else if (strncmp("Latest proxy authen enable", temp, 24) == 0) {
			line = 0;
			while (line<255) {
				if (feof(fn)) goto loadheader_exit;
				c = getc(fn);
				if ((c != ' ') && (c != '\t')) 
					break;
				line++;
			}
			if ((c == '\n') || (c == '\r')) {
				continue;
			}
			
			fseek(fn, -1, SEEK_CUR);
			int getvalue = 0;
			fscanf(fn, "%d\n", &getvalue);
			g_ProxyAuthEnable = (bool)getvalue;
			//cout << "+++++g_ProxyEnable:" << g_ProxyAuthEnable << endl;
		}
		if (feof(fn)) break;
	}
	return TRUE;
	
loadheader_exit:
	fclose(fn);
	return FALSE;
}


/********************************************************************************************************************
 *****Function: SaveStoriesMap
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
bool SaveStoriesMap () {
	//FILE *fn;
	string mapFilePath = g_System_WorkspaceDir + "/Links/stories_map.json";
	ofstream File(mapFilePath.c_str());
	string storiesMapStr = JSONHelper::storiesMapString();
	File << storiesMapStr;
	return TRUE;
}


/********************************************************************************************************************
 *****Function: SaveImagesMap
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
bool SaveImagesMap() {

	//FILE *fn;

	string mapFilePath = g_System_WorkspaceDir + "/Links/images_map.json";
	ofstream File(mapFilePath.c_str());
	
	/*
	fn = fopen(mapFilePath.c_str(), "wb");
	if (fn == NULL) {
		fclose(fn);
		return FALSE;
	}*/
	
	string imagesMapStr = JSONHelper::imagesMapString();
	cout << "Image Info:" << imagesMapStr << endl;
	//fprintf(fn, "%s", storiesMapStr.c_str());
	//fclose(fn);
	File << imagesMapStr;
	
	//string test = JSONHelper::imagesMapString();
	//cout << "---------image map ----------------" << endl;
	//cout << test << endl;
	//cout << "-----------------------------------" << endl;
	return TRUE;
}


/********************************************************************************************************************
 *****Function: SaveLinksMap
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
bool SaveLinksMap() {
	
	//FILE *fn;
	
	string mapFilePath = g_System_WorkspaceDir + "/Links/links_map.json";
	ofstream File(mapFilePath.c_str());
	
	string imagesMapStr = JSONHelper::linksMapString();
	File << imagesMapStr;
	return TRUE;
}


/********************************************************************************************************************
 *****Function: SaveLogCenterData
 *****Creator:  Toan Nguyen
 *****IN: string _content
 *****out: 
 *****Objective: save log center
 ********************************************************************************************************************/
bool SaveLogCenterData(string _content)
{
	int32 hour, minute, second;
	int32 year, month, day;
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	
	//Read year month and day
	year = timeinfo->tm_year + 1900;
	month = timeinfo->tm_mon + 1;
	day = timeinfo->tm_mday;
	//Read Hour minute and second
	//hour = timeinfo->tm_hour;
	//minute = timeinfo->tm_min;
	//second = timeinfo->tm_sec;
	
	string _time_in_;
	stringstream _time_out_;
	_time_out_ << "[";
	_time_out_ << year;
	_time_out_ << ":";
	_time_out_ << month;
	_time_out_ << ":";
	_time_out_ << day;
	_time_out_ << "] Audience Media Log: ";
	_time_in_ = _time_out_.str();
	
	
	string _logdata = _time_in_ + _content + "\n";
	string mapFilePath = g_System_logDirectory + "/logcenter.txt";
	fstream File(mapFilePath.c_str(), ios::in | ios::out);
	if(File.is_open()){
		//_logdata << File;
		//cout << "write log center" << endl;
		File.seekg (0, ios::end);
		File << _logdata;
		File.close();
	}else
	{
		//cout << "Can't open log center" << endl;
		return FALSE;
	}
	return TRUE;
}


/********************************************************************************************************************
 *****Function: InitLogCenter
 *****Creator:  Toan Nguyen
 *****IN: bool EnableClear
 *****out: 
 *****Objective: init log center
 ********************************************************************************************************************/
void InitLogCenter(bool EnableClear)
{
	string mapFilePath = g_System_logDirectory + "/logcenter.txt";
	ofstream File(mapFilePath.c_str());
	
	if(EnableClear == TRUE) {
		File << "";				// Clear all content data before
		SaveLogCenterData("------------Progress working start------------");
	}
	File.close();
	
	string excutePath = "chflags hidden " + mapFilePath;
	system(excutePath.c_str());
}